package com.exception.demo.response;

import lombok.Getter;
import lombok.Setter;

/**
 * 
 * @ClassName: ResultObj
 * @Description: 返回前端对象
 * @param <T>
 * @author ldq
 * @date 2020-03-28 18:17:26
 */
@Getter
@Setter
public class ResultObj<T> implements Result {

	/**
	 * @Fields serialVersionUID : UID
	 */
	private static final long serialVersionUID = -7927883863875787432L;
	
	/**
	 *	响应状态码
	 */
	private Integer code;
	
	/**
	 * 	错误信息
	 */
	private String message;
	
	/**
	 *	返回数据
	 */
	private T data;
	
	/**
	 * ResultObj构造函数
	 */
	private ResultObj() {
		this.code = ResultCode.SUCCESS.getCode();
	}
	
	/**
	 * ResultObj构造函数
	 */
	private ResultObj(Integer code, String message) {
		this.code = code;
		this.message = message;
	}
	
	/**
	 * ResultObj构造函数
	 */
	private ResultObj(ResultCode resultCode) {
		this.code = resultCode.getCode();
		this.message = resultCode.getMessage();
	}
	
	/**
	 * ResultObj构造函数
	 */
	private ResultObj(ResultCode resultCode, T data) {
		this.code = resultCode.getCode();
		this.message = resultCode.getMessage();
		this.data = data;
	}
	
	/**
	 * ResultObj构造函数
	 */
    private ResultObj(T data) {
        this.code = ResultCode.SUCCESS.getCode();
        this.data = data;
    }
    
    /**
     * @Title: success
     *	通用返回成功（没有返回结果）  
     */
    public static<T> ResultObj<T> success(){
        return new ResultObj<T>();
    }

    /**
     * @Title: success
     *	返回成功（有返回结果）
     * @return: ResultObj<T>
     */
    public static<T> ResultObj<T> success(T data){
    	
        return new ResultObj<T>(data);
    }
    
    /**
     * @Title: failure
     * 	 返回失败
     * @return: ResultObj<T>
     */
    public static<T> ResultObj<T> failure(ResultCode resultCode) {
    	
		return new ResultObj<T>(resultCode);
	}
    
    /**
     * @Title: failure
     * 	返回失败
     * @return: ResultObj<T>
     */
    public static<T> ResultObj<T> failure(ResultCode resultCode, T data) {
    	
		return new ResultObj<T>(resultCode, data);
	}
    
    /**
     * @Title: failure
     * 	返回失败
     * @return: ResultObj<T>
     */
    public static<T> ResultObj<T> failure(Integer code, String message) {
    	
		return new ResultObj<T>(code, message);
	}
	
	@Override
	public String toString() {
		return "{\"code\":\"" + code + "\",\"message\":\"" + message + "\",\"data\":\"" + data + "\"}";
	}

}
